package com.adong.web;

import com.adong.pojo.User;
import com.adong.service.UserService;
import com.adong.service.impl.UserServiceImpl;
import com.adong.utils.JdbcUtils;
import com.adong.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * 这个类专门封装登录注册表的具体验证方法
 *
 * @author adong
 */
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 登录的验证具体操作
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = userService.loginUser(new User(null, username, password, null));
        //判断用户名和密码正确
        if (user == null) {
            //密码或者用户名没有找到
            req.setAttribute("message", "用户名或者密码错误");
            req.setAttribute("userName", username);

            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);

        } else {
            req.getSession().setAttribute("user", user);
            //找到用户名和密码
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
        }


    }

    /**
     * 注册表验证操作
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取 Session 中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        // 删除 Session 中的验证码, 返回的注册的话里面的里面的验证码为null通过不了验证
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        String repwd = req.getParameter("repwd");
        String code = req.getParameter("code");

        //封装注册的信息封装到javaBean对象中去中去
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        req.setAttribute("user", user);
        //检查验证码不为空以及是否正确
        if (token != null && code.equalsIgnoreCase(token)) {

            //检查是否存在用户名
            if (userService.checkUserName(user.getUsername())) {

                //存在
                req.setAttribute("qpassWord", repwd);
                req.setAttribute("user", user);
                req.setAttribute("msg", "用户名已存在");
                //请求转发
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
            } else {
                //不存在,注册成功
                req.getSession().setAttribute("user", user); //保存在Session域中让后面页面可以获取名字
                resp.sendRedirect(req.getContextPath() + "/pages/user/regist_success.jsp"); //使用重定向解决用户按下F5再次发起请求
//                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
                userService.registerUser(user);
            }

        } else {
            //验证码为空,或者错误的情况
            //保存数据,便于传输的jsp页面上去再次显示出来
            req.setAttribute("user", user);
            req.setAttribute("qpassWord", repwd);

            if (code == null || code == "") {
                req.setAttribute("msg", "请输入验证码");
            } else {
                req.setAttribute("msg", "验证码错误");
            }
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
        }

    }

    /**
     * 注销操作
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁session信息,销毁信息的 req.getSession()中的Session存储的user对象就清除了
        req.getSession().invalidate();
        //重定向到首页
        resp.sendRedirect(req.getContextPath());
    }

}
